//
//  ATVSelectPropertyViewController.m
//  FlashController
//
//  Created by Martin Schiøth Dyrby on 8/7/12.
//  Copyright 2012 Molamil. All rights reserved.
//

#import "ATVSelectPropertyViewController.h"
#import "ATVInstanceSelectProperty.h"

@implementation ATVSelectPropertyViewController

@synthesize titleLabel;
@synthesize popUpButton;
@synthesize curValue;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        curValue = @"";
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateValueHandler:) name:@"updateValue" object:nil];

    }
    
    
    return self;
}

- (void) updateValueHandler: (NSNotification*) note {
    
    NSDictionary* dict = (NSDictionary*)note.object;
    NSString* path = [dict valueForKey:@"path"];
    
    if([path isEqualToString:self.path]) {
        curValue = [dict valueForKey:@"value"];
        
        [self updatePopUpButton];
    }
}

- (void) drawFromModel:(ATVInstanceSelectProperty*) model {
    [titleLabel setStringValue: model.name];
    [popUpButton removeAllItems];
    if([model.name isEqualToString:@"blendMode"]) {

        [popUpButton addItemsWithTitles:[[NSArray alloc] initWithObjects:@"add", @"alpha",@"darken",@"difference",@"erase",@"hardlight",@"invert",@"layer",@"lighten",@"multiply",@"normal",@"overlay",@"screen",@"shader",@"subtract", nil]];
    }
    
    [self updatePopUpButton];
}

- (void) updatePopUpButton {
    [popUpButton selectItemWithTitle:curValue];
}

- (IBAction)popUpButtonMessage:(id)sender {
    curValue = [[popUpButton selectedItem] title];
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:curValue, @"value", self.path, @"path", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"setSimpleValue" object:dict];
}
@end
